// pages/my/giving/givingG/givingG.js
import {
	$wuxKeyBoard
} from '../../../../dist/index'

var app = getApp();
var $ = app.globalData;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tips_warn: false,
		tips_primary: false,
		errortext: "发生未知错误"
	},
	// 输入密码
	open() {
		var that = this
		if (!that.data.loginname) {
			that.setData({
				tips_warn: true,
				errortext: "请输入正确的用户名"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else if (!that.data.thisnub || parseInt(that.data.thisnub) == 0) {
			that.setData({
				tips_warn: true,
				errortext: "请输入正确的数量"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else {
			$wuxKeyBoard().show({
				className: 'givingBut',
				inputText: '请输入支付密码',
				disorder: true,
				callback(value) {
					wx.getStorage({
						key: "userinfo",
						success: function(res) {
							wx.request({
								url: $.AdminUrl + "/YApi/giveMoney",
								data: {
									customerid: res.data.id,
									loginname: that.data.loginname,
									cashmoney: 0,
									integralmoney: that.data.thisnub,
									paypassword: `${value}`
								},
								success: function(res) {
									if (res.data.code) {
										wx.showToast({
											title: res.data.msg,
											duration: 3000,
										})
										that.getuserinfo()
										that.getjilu()
									} else {
										wx.showToast({
											icon:'none',
											title: res.data.msg,
											duration: 3000,
										})
									}
								}
							})
						}
					})
					return true
				},
			})
		}

	},
	// 获取用户信息
	loginname: function(e) {
		// console.log(e.detail.value)
		var loginnanme = e.detail.value
		this.setData({
			loginname: loginnanme
		})
		var that = this
		wx.request({
			url: $.AdminUrl + "/YApi/getMySelfByLoginname",
			data: {
				loginname: loginnanme
			},
			success: function(res) {
				if (res.data.obj != null) {
					var thisphone = res.data.obj.phone
					thisphone = thisphone.slice(thisphone.length - 4, thisphone.length)
					that.setData({
						thisname: res.data.obj.name,
						thisloginname: res.data.obj.loginname,
						thisphone: thisphone
					})
				}else{
					that.setData({
						thisname: "",
						thisloginname:"",
						thisphone:""
					})
				}
				console.log(res.data)
			}
		})
	},
	// 转增数量
	zznub: function(e) {
		var thisnub = e.detail.value
		this.setData({
			thisnub: thisnub
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		that.getuserinfo()
		that.getjilu()
	},
	// 获取本人信息
	getuserinfo: function() {
		var that = this
		// 获取用户信息
		wx.getStorage({
			key: "userinfo",
			success: function(data) {
				wx.request({
					url: $.AdminUrl + "/YApi/getMySelfByLoginname",
					data: {
						loginname: data.data.loginname
					},
					success: function(res) {
						that.setData({
							userinfo: res.data.obj
						})
						wx.setStorage({
							key: "userinfo",
							data: res.data.obj
						})
					}
				})
			},
			fail: function(res) {
				// 用户信息获取失败
			}
		})
	},
	// 获取转增记录
	getjilu: function() {
		var that = this
		wx.request({
			url: $.AdminUrl + "/YApi/getMyWallet",
			data: {
				customerid: wx.getStorageSync("userinfo").id,
				state: 4,
			},
			success: function(data) {
				var jilulist=data.data.obj
				for(var i=0;i<jilulist.length;i++){
					var thime = jilulist[i].updateTime;
					var givename = jilulist[i].remark;
					var givenameA = givename.indexOf('[');
					var givenameB = givename.indexOf(']');
					givename = givename.substr(givenameA + 1, givenameB - givenameA - 1);
					jilulist[i]['uptime'] = thime.substring(0,10)
					jilulist[i]['givename'] = givename
				}
				console.log(jilulist)
				that.setData({
					jilulist:jilulist
				})
			}
		})
	},
	// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
})
